﻿/// <reference path="POCO/Contact.js" />
/// <reference path="../jquery/jquery-1.3.2-vsdoc.js" />
/// <reference path="Complaint.Namespaces.js" />
/// <reference path="Complaint.Common.js" />
/// Dependency: Script block from Contact.ascx

var URL = new String();

$(function() {
    //hover states on the static widgets
    $("#addContact, .ui-icon-close").hover(
					function() { $(this).addClass('ui-state-hover'); },
					function() { $(this).removeClass('ui-state-hover'); }
				);

    $("#contactDialog").dialog({
        autoOpen: false,
        bgiframe: true,
        modal: true,
        height: 350,
        width: 400,
        buttons:
                {
                    "Cancel": function() {
                        $(this).dialog("close");
                    },
                    "OK": function() {
                        //var data = $("div.form input").serialize();
                        if (!$("form#contact").validate().form())
                            return false;
                        var contact = new Complaint.POCO.Contact(
                                $("div.form select#Title").val(),
                                $("div.form input#FirstName").val(),
                                $("div.form input#LastName").val(),
                                $("div.form input#JobTitle").val(),
                                $("div.form input#Email").val()
                                );
                        contacts.push(contact);
                        // Create organisation
                        if (isCreate) {
                            addContactForNewOrg(contact);
                        }
                        // Edit organisation
                        else {
                            var obj = {
                                type: "POST",
                                dataType: "html",
                                timeout: 30000,
                                url: URL,
                                data: { json: contact.toJson() },
                                error: errorhandler,
                                success: function(data) {
                                    $("#contactGrid").html(data);
                                    $("#contactDialog").dialog("close");
                                }
                            };
                            $.ajax(obj);
                        }
                    }
                }
    });

    $("#addContact").click(function(evt) {
        evt.preventDefault();
        URL = Complaint.Common.SiteRoot + "/Organisation/AddContact/" + organisationId;
        $.get(
                    URL,
                    {},
                    function(data) {
                        $("#contactDialog").html(data);
                        $("#contactDialog").dialog("open");
                    }
                );
    });

    Array.prototype.addEle = function(ele, value) {
        this.push("<");
        this.push(ele);
        this.push(">");
        this.push(value);
        this.push("</");
        this.push(ele);
        this.push(">");
    };

    var addContactForNewOrg = function(contact) {
        if (contacts.length == 1) {
            var arr = ["Title", "First Name", "Last Name", "Job Title", "Email", " "];
            var tableHeader = new Array();
            for (var i = 0, item; item = arr[i]; i++) {
                tableHeader.addEle("th", item);
            }
            $("#tblContacts thead tr").empty().append(tableHeader.join(""));
            $("#tblContacts tbody").empty();
        }

        var newRow = $("<tr />").addClass("gridRowSelectable");
        var tableRow = new Array();
        tableRow.addEle("td", contact.Title);
        tableRow.addEle("td", contact.FirstName);
        tableRow.addEle("td", contact.LastName);
        tableRow.addEle("td", contact.JobTitle);
        tableRow.addEle("td", contact.Email);
        tableRow.addEle("td", "<a href=\"javascript:del(" + (contacts.length - 1) + ")\">Delete</a>");

        $(newRow).append(tableRow.join(""));
        $(newRow).appendTo($("#tblContacts tbody"));
        $("#contactDialog").dialog("close");
    };

});

var errorhandler = function(XMLHttpRequest, textStatus, errorThrown) {
    $("#contactDialog").html(XMLHttpRequest.responseText);
};

var edit = function(id) {
    if (isCreate) {
        URL = Complaint.Common.SiteRoot + "/Organisation/AddContact/" + organisationId;
    }
    else {
        URL = Complaint.Common.SiteRoot + "/Organisation/EditContact/" + id;
    }
    var obj = {
        type: "GET",
        dataType: "html",
        timeout: 30000,
        url: URL,
        error: errorhandler,
        success: function(data) {
            $('#contactDialog').html(data);
            if (isCreate) {
                $("div.form input#Title").val();
                $("div.form input#FirstName").val();
                $("div.form input#LastName").val();
                $("div.form input#JobTitle").val();
                $("div.form input#Email").val();
            }
            $('#contactDialog').dialog('open');
        }
    };
    $.ajax(obj);
}

var del = function(id) {
    if (isCreate) {
        contacts.splice(id, 1);
        $("#tblContacts tbody tr:eq(" + id + ")").remove();
    }
    else {
        URL = Complaint.Common.SiteRoot + "/Organisation/DeleteContact/" + id;
        var obj = {
            type: "GET",
            dataType: "html",
            timeout: 30000,
            url: URL,
            data: { organisationId: organisationId },
            error: function(XMLHttpRequest, textStatus, errorThrown) {
                alert(XMLHttpRequest.responseText);
            },
            success: function(data) {
                $("#contactGrid").html(data);
            }
        };
        $.ajax(obj);
    }
}
